#!/bin/bash

# 环境设置
echo -e "\t\t\033[32m 关闭防火墙 \033[0m"
systemctl stop firewalld
systemctl disable firewalld 
echo -e "\033[32m firewalld开机状态：\033[0m" "\033[31m $(systemctl status firewalld | tr -d " " | awk -F ";" '/^Loaded/{print $2}') \033[0m"
echo -e "\033[32m firewalld运行状态：\033[0m" "\033[31m $(systemctl status firewalld | tr -d " " | awk -F ":" '/^Active/{print $2}') \033[0m"


# 关闭SELINUX
sed -i '/^SELINUX/s/enforcing/disabled/' /etc/selinux/config
echo -e "\033[32m SELINUX状态 \033[0m" " \033[31m $(awk '/^SELINUX=/{print $0}' /etc/selinux/config) \033[0m "


# 修改主机名为Discuz
hostnamectl set-hostname Discuz
echo -e "\033[32m 主机名为：\033[0m" "\033[31m $( hostname ) \033[0m"


# 卸载原有数据库环境
mysql_server=$( rpm -qa | grep -i mysql-community )
if [ -n "$mysql_server" ];then
        rpm -e --nodeps $(rpm -qa | grep -i mysql-community )
    echo -e "\033[32m mysql：\033[0m \033[31m 已卸载！\033[0m"
else
        echo -e "\033[32m mysql：\033[0m \033[31m 未安装！\033[0m"
fi
mariadb_server=$( rpm -qa | grep -i MariaDB )
if [ -n "$mariadb_server" ];then
        # -n 参数判断字符串是不是空值，为空就是rpm -qa mariadb运行成功，但是mysql没有安装，不为空就是rpm -qa mariadb运行成功返回系统已安装mysql版本
        rpm -e --nodeps $(rpm -qa | grep -i mariadb)
    echo -e "\033[32m mariadb：\033[0m \033[31m 已卸载！\033[0m"
else
        echo -e "\033[32m mariadb：\033[0m \033[31m mariadb未安装！\033[0m"
fi


#安装基础环境
echo 
echo -e "\t\t\033[32m 安装基础环境 \033[0m"
yum -y install yum-utils &> /dev/null
yum_utils=$( rpm -qa | grep -i yum-utils )
if [ -n "$yum_utils" ];then
    echo -e "\033[32m yum-utils：\033[0m \033[31m ✔ 安装成功！\033[0m"
else
    echo -e "\033[32m yum-utils：\033[0m \033[31m ✘ 安装失败！\033[0m"
fi
yum -y install net-tools &> /dev/null
yum_tools=$( rpm -qa | grep -i net-tools )
if [ -n "$yum_tools" ];then
        echo -e "\033[32m net-tools：\033[0m \033[31m ✔ 安装成功！\033[0m"
else
        echo -e "\033[32m net-tools：\033[0m \033[31m ✘ 安装失败！\033[0m"
fi

yum -y install unzip &> /dev/null
yum_unzip=$( rpm -qa | grep -i unzip )
if [ -n "$yum_unzip" ];then
        echo -e "\033[32m unzip：\033[0m \033[31m ✔ 安装成功！\033[0m"
else
        echo -e "\033[32m unzip：\033[0m \033[31m ✘ 安装失败！\033[0m"
fi

yum -y install httpd &> /dev/null
yum_httpd=$( rpm -qa | grep -i httpd )
if [ -n "$yum_httpd" ];then
        echo -e "\033[32m httpd：\033[0m \033[31m ✔ 安装成功！\033[0m"
else
        echo -e "\033[32m httpd：\033[0m \033[31m ✘ 安装失败！\033[0m"
fi



# 设置httpd开机启动 
systemctl enable httpd &>/dev/null
systemctl start httpd
echo -e "\033[32m httpd开机状态：\033[0m" "\033[31m $(systemctl status httpd | tr -d " " | awk -F ";" '/^Loaded/{print $2}')  \033[0m"
echo -e "\033[32m httpd运行状态：\033[0m" "\033[31m $(systemctl status httpd | tr -d " " | awk -F ":" '/^Active/{print $2}')  \033[0m"


#安装mysql
echo
echo -e "\t\t \033[32m安装mysql \033[0m"

#下载mysql
wget -P /tmp/ https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-5.7.40-1.el7.x86_64.rpm &> /dev/null
wget -P /tmp/ https://downloads.mysql.com/archives/get/p/23/file/mysql-community-common-5.7.40-1.el7.x86_64.rpm &> /dev/null
wget -P /tmp/ https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-5.7.40-1.el7.x86_64.rpm &> /dev/null
wget -P /tmp/ https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm &> /dev/null
wget -P /tmp/ https://downloads.mysql.com/archives/get/p/23/file/mysql-community-server-5.7.40-1.el7.x86_64.rpm &> /dev/null

#赋予执行权限
chmod +x /tmp/mysql-community*

#安装mysql
rpm -i --force --nodeps --nosignature /tmp/mysql-community-common-5.7.40-1.el7.x86_64.rpm
rpm -i --force --nodeps --nosignature /tmp/mysql-community-libs-5.7.40-1.el7.x86_64.rpm
rpm -i --force --nodeps --nosignature /tmp/mysql-community-libs-compat-5.7.40-1.el7.x86_64.rpm
rpm -i --force --nodeps --nosignature /tmp/mysql-community-client-5.7.40-1.el7.x86_64.rpm
rpm -i --force --nodeps --nosignature /tmp/mysql-community-server-5.7.40-1.el7.x86_64.rpm
#-i 安装 RPM 包 -v 显示详细安装过程 -h 以进度条形式展示安装进度 --force 强制覆盖已安装的版本 --nodeps 忽略依赖检查，直接安装 --nosignature 跳过对 RPM 包签名的验证

#权限重置
rm -rf /var/log/mysqld.log
touch /var/log/mysqld.log
chown mysql:mysql /var/log/mysqld.log
chmod 640 /var/log/mysqld.log
chmod 755 /var/log

#初始化mysql
systemctl stop mysqld
rm -rf /var/lib/mysql/*
mysqld --initialize --user=mysql

#mysql开机启动
systemctl start mysqld
systemctl enable mysqld  &>/dev/null
echo -e "\033[32m mysqld开机状态：\033[0m" "\033[31m $(systemctl status mysqld | tr -d " " | awk -F ";" '/^Loaded/{print $2}')  \033[0m"
echo -e "\033[32m mysqld运行状态：\033[0m" "\033[31m $(systemctl status mysqld | tr -d " " | awk -F ":" '/^Active/{print $2}')  \033[0m"

#查看密码
mysql_pass=$( grep 'temporary password' /var/log/mysqld.log | awk '{print $11}' | tr -d ' ' )
echo -e "\033[32m mysql初始密码：\033[0m" "\033[31m $mysql_pass \033[0m"
mysql_Passwd="Discuz123@"

# 登录 MySQL 并修改密码，
mysql -u root -p"$mysql_pass" --connect-expired-password 2>/dev/null <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED BY '$mysql_Passwd';
FLUSH PRIVILEGES;
EOF
echo -e "\033[32m mysql新密码：\033[0m" "\033[31m $mysql_Passwd \033[0m"

# 使用新密码登录 MySQL 并创建数据库
mysql -u root -p"$mysql_Passwd" 2>/dev/null <<EOF
CREATE DATABASE discuz CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
EOF

#检查数据库创建
mysql_discuz=$( mysql -uroot -p'Discuz123@' -e "SHOW DATABASES;" 2>/dev/null | grep discuz )
if [ -n "$mysql_discuz" ];then
    echo -e "\033[32m discuz数据库： \033[0m \033[31m ✔ 创建成功！\033[0m"
else
    echo -e "\033[32m discuz数据库： \033[0m \033[31m ✘ 创建失败，请手动创建！\033[0m"
fi



#安装PHP
echo 
echo -e "\t\t \033[32m 安装PHP \033[0m"

#下载安装remi-release-7.rpm
wget -P /tmp/ https://mirrors.aliyun.com/remi/enterprise/remi-release-7.rpm &> /dev/null
wget -P /tmp/ https://mirrors.aliyun.com/centos/7.9.2009/extras/x86_64/Packages/epel-release-7-11.noarch.rpm &> /dev/null
chmod +x /tmp/*.rpm
rpm -ivh /tmp/epel-release-7-11.noarch.rpm &>/dev/null
rpm_noarch=$( rpm -qa | grep -i epel-release-7-11.noarch )
if [ -n "$rpm_noarch" ];then
    echo -e "\033[32m epel-release-7-11.noarch：\033[0m \033[31m ✔ 安装成功！\033[0m"
else
    echo -e "\033[32m epel-release-7-11.noarch：\033[0m \033[31m ✘ 安装失败！\033[0m"
fi
rpm -ivh /tmp/remi-release-7.rpm &>/dev/null
rpm_release=$( rpm -qa | grep -i remi-release-7 )
if [ -n "$rpm_release" ];then
        echo -e "\033[32m remi-release-7：\033[0m \033[31m ✔ 安装成功！\033[0m"
else
        echo -e "\033[32m remi-release-7：\033[0m \033[31m ✘ 安装失败！\033[0m"
fi

#清理yum缓存
yum clean all &>/dev/null
yum_makecache=$( yum makecache fast | grep "元数据缓存已建立" )
if [ -n "$yum_makecache" ];then
        echo -e "\033[32m yum源：\033[0m \033[31m ✔ 元数据缓存已建立！\033[0m"
else
        echo -e "\033[32m yum源：\033[0m \033[31m ✘ 元数据缓存建立失败！\033[0m"
fi

#启用PHP80的yum源
yum-config-manager --enable remi-php80 &>/dev/null

#安装php80 

# 定义PHP包列表
packages=(
    php80
    php80-php
    php80-php-xml
    php80-php-xmlrpc
    php80-php-pecl-mcrypt
    php80-php-fpm
    php80-php-pecl-apcu
    php80-php-mbstring
    php80-php-gd
    php80-php-json
    php80-php-pecl-json-post
    php80-php-pdo
    php80-php-mysqlnd
    php80-php-pecl-mysql
    php80-php-opcache
    php80-php-pear
    php80-php-soap
    php80-php-intl
    php80-php-pear
    php-pear-MDB2-Driver-mysqli.noarch
)

# 记录失败的包
failed_packages=()

# 循环安装每个包
for package in "${packages[@]}"; do
    #echo -e "\033[32m 正在安装包: $package \033[0m"
    
    # 执行安装并捕获输出
    install_output=$(yum -y install "$package" &>/dev/null)
    install_status=$?
    
    # 检查安装状态
    if [ $install_status -eq 0 ]; then
        echo -e "\033[32m ✔ 包 $package 安装成功 \033[0m"
    else
        echo -e "\033[31m ✘ 包 $package 安装失败 \033[0m"
        echo -e "\033[33m 错误详情: \033[0m"
        echo "$install_output" | tail -n 10  # 显示最后10行错误信息
        failed_packages+=("$package")
    fi
    
    echo "----------------------------------------"
done


# 输出安装总结
echo
echo -e "\033[32m 所有PHP包安装尝试完成! \033[0m"

if [ ${#failed_packages[@]} -eq 0 ]; then
    echo -e "\033[32m ✔ 所有包均安装成功 \033[0m"
else
    echo -e "\033[31m ✘ 以下包安装失败: \033[0m"
    for failed in "${failed_packages[@]}"; do
        echo -e "\033[31m - $failed \033[0m"
    done
fi


#启动php
systemctl enable php80-php-fpm &>/dev/null
systemctl start php80-php-fpm
echo -e "\033[32m php80开机状态：\033[0m" "\033[31m $(systemctl status php80-php-fpm | tr -d " " | awk -F ";" '/^Loaded/{print $2}')  \033[0m"
echo -e "\033[32m php80运行状态：\033[0m" "\033[31m $(systemctl status php80-php-fpm | tr -d " " | awk -F ":" '/^Active/{print $2}')  \033[0m"

#重启httpd
systemctl restart httpd


#安装Discuz
echo 
echo -e "\t\t \033[32m安装Discuz \033[0m"
mkdir /tmp/discuz
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m discuz目录：\033[0m\033[31m discuz目录创建失败！\033[0m"
else
    echo -e "\033[32m discuz目录：\033[0m\033[31m discuz目录创建成功！\033[0m"
fi

wget -P /tmp/discuz/ https://gitee.com/Discuz/DiscuzX/attach_files/2044572/download/Discuz_X3.5_SC_UTF8_20250205.zip &>/dev/null
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m discuz源文件：\033[0m\033[31m discuz源文件下载失败！\033[0m"
else
    echo -e "\033[32m discuz源文件：\033[0m\033[31m discuz源文件下载成功！\033[0m"
fi

unzip -o /tmp/discuz/Discuz_X3.5_SC_UTF8_20250205.zip -d /tmp/discuz/ &>/dev/null
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m discuz源文件解压：\033[0m\033[31m discuz解压失败！\033[0m"
else
    echo -e "\033[32m discuz源文件解压：\033[0m\033[31m discuz解压成功！\033[0m"
fi

cp -r /tmp/discuz/upload/* /var/www/html/
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m discuz源文件部署：\033[0m\033[31m discuz源文件部署失败！\033[0m"
else
    echo -e "\033[32m discuz源文件部署：\033[0m\033[31m discuz源文件部署成功！\033[0m"
fi

chmod -R 777 /var/www/html/
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m html文件授权：\033[0m\033[31m html文件授权失败！\033[0m"
else
    echo -e "\033[32m html文件授权：\033[0m\033[31m html文件授权成功！\033[0m"
fi


# 检查 httpd 服务是否活跃（运行中）
systemctl restart httpd &>/dev/null
systemctl is-active httpd &>/dev/null
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m httpd状态：\033[0m\033[31m httpd重启失败！\033[0m"
else
    echo -e "\033[32m httpd状态：\033[0m\033[31m httpd重启成功！\033[0m"
fi

#验证discuz
wget --spider http://localhost/install/ &>/dev/null
if [ $? -ne 0 ]; then
    #$?上一个命令返回值 -ne 正则表达式 不等于 
    echo -e "\033[32m discuz状态：\033[0m\033[31m discuz部署失败！\033[0m"
else
    echo -e "\033[32m discuz状态：\033[0m\033[31m discuz部署成功！\033[0m"
    ip_ip=$(ip a | awk 'NR==9{print $2}'| awk -F "/" '{print $1}')
    echo -e "\033[31m discuz部署完成！本地网络访问 http://localhost/install 完成进一步设置。 \033[0m"
    echo -e "\033[31m discuz部署完成！其它网络访问 http://$ip_ip/install 完成进一步设置。 \033[0m"
fi


